<template>
  <div class="updatePassword">
    <el-dialog title="修改登陆密码" :visible.sync="dialogVisibleforedit" width="30%">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px">
        <el-form-item label="初始密码" prop="oldPassword">
          <el-input v-model="form.oldPassword" show-password clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="form.newPassword" show-password clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="repeatPassword">
          <el-input v-model="form.repeatPassword" show-password clearable style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleforedit = false">取 消</el-button>
        <el-button type="primary" @click="editSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import request2 from "@/utils/request2";

export default {
  name: "updatePassword",
  data() {
    return {
      dialogVisibleforedit: false,
      tno: JSON.parse(Cookies.get('teacher')).username,
      form: {},
      rules: {
        oldPassword: [
          {required: true, message: '请输入初始密码', trigger: 'blur'},
        ],
        newPassword: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
        ],
        repeatPassword: [
          {required: true, message: '请确认密码', trigger: 'blur'},
        ],
      }
    }
  },
  created() {
    this.dialogVisibleforedit = true
  },
  methods: {
    editSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request2.get('/teacherOfTeacher/checkPassword/' + this.tno + '/' + this.form.oldPassword).then(res => {
            if (res.code == '200') {
              if (this.form.newPassword !== this.form.repeatPassword) {
                this.$notify.error('新密码和确认密码不一致')
              } else {
                request2.put('/teacherOfTeacher/updatePassword/' + this.tno + '/' + this.form.newPassword).then(res => {
                  if (res.code == '200') {
                    this.$notify.success('修改成功')
                    this.dialogVisibleforedit = false
                  } else {
                    this.$notify.error('修改失败')
                  }
                })
              }
            } else {
              this.$notify.error('初始密码错误')
            }
          })
        } else {
          this.$notify.warning('请填写全部信息')
        }
      })
    },
  }
}
</script>
